package com.project_doctor.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project_doctor.pojo.Health;
import com.project_doctor.pojo.dto.HealthInfoDTO;
import com.project_doctor.service.HealthService;
import com.project_doctor.mapper.HealthMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author xixif
* @description 针对表【t_health(居民健康信息表)】的数据库操作Service实现
* @createDate 2025-06-09 12:35:42
*/
@Service
public class HealthServiceImpl extends ServiceImpl<HealthMapper, Health>
    implements HealthService{
    @Autowired
    private HealthMapper healthMapper;
    @Autowired
    private ContractServiceImpl contractService;

    @Override
    @Transactional
    public void updateHealth(HealthInfoDTO dto) {
        Health health = new Health();
        health.setResidentId(dto.getResidentId());
        health.setBloodPressure(dto.getBloodPressure());
        health.setFastingBloodGlucose(dto.getFastingBloodGlucose());
        health.setHeartRate(dto.getHeartRate());
        health.setConditionDescription(dto.getConditionDescription());
        health.setHealthGuidance(dto.getHealthGuidance());

        LambdaUpdateWrapper<Health> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Health::getResidentId, dto.getResidentId());
        updateWrapper.eq(Health::getResidentId, dto.getResidentId());

        // 自动根据wrapper条件判断是否存在
        this.saveOrUpdate(health, updateWrapper);

        contractService.updateComplete(dto.getContractId());
    }
}




